<?php

namespace app\app\controller;


use app\model\CardModel;
use app\model\DeviceModel;
use app\model\EnterLogModel;
use app\model\MenuModel;
use think\App;
use think\Controller;
use app\model\AppUserModel;
use app\model\EquipmentModel;
use Common_util_pub;
use think\Request;
class Core extends Common
{
    /**
     * @return \think\response\View
     * 个人中心
     */
    public function index(){
        $this->assign('nickname',session('appuser.nickname'));//用户昵称
        $this->assign('user_id',session('appuser.id'));//用户id
        return view('core');
    }
    //设备管理
    public function devedit(){
        $equ_name=input('get.imei');
        $data['data']=EquipmentModel::where('name',$equ_name)->find();

        return view('devedit',$data);
    }
    //设备资料更新--只能操作设备昵称
    public function upEqu()
    {
        $getData=input('post.');
        $getEqu=EquipmentModel::find($getData['id']);
        if ($getEqu->equ_user!=session('appuser.id')){$this->error('非法操作');}
        $updata=[
            'id'=>$getData['id'],
            'nickname'=>$getData['nickname']];
        $file = request()->file('img_head');//单图--头像
        if ($file){//商品图片是否上传（单图）
            $updata['head']=$this->upload_files($file);
            if ($getEqu->head){//1.检测原有图片文件
                $this->del_file(session('appuser.head'));//2.清除原有图片文件
            }
        }
        EquipmentModel::update($updata);
        $this->redirect('Index/device');
    }
    //解除绑定
    public function unbind()
    {
        $equ_name=input('post.name');
        $setEqu=EquipmentModel::where('name',$equ_name)->update(['equ_user'=>0]);
        setDatabaseDef($setEqu);
        $this->upSess_dev();
        $this->upSess_dev_nik();
        return setDatabase($setEqu,'解绑成功');
    }
    //设备添加页面
    public function add()
    {
        return view('add');
    }
    //设备添加
    public function addData()
    {
        $getData=input('post.');

        //查找设备
        $where=['name'=>$getData['imei']];
        $getEqu=EquipmentModel::where($where)->find();
        setDatabaseDef($getEqu,'请检查设备号');
        if($getEqu->equ_user!=0){
            retJson('',201,'该设备已被绑定')->send();
            exit();
        }
        $update=['equ_user'=>session('appuser.id')];
        $setEqu= EquipmentModel::update($update,['id'=>$getEqu->id]);//绑定
        $this->upSess_dev();
        $this->upSess_dev_nik();
        return setDatabase($setEqu,'绑定');
    }
    //个人资料
    public function user(){
        $data['data']=session('appuser');
//        dump(session('dev_name'));
//        dump($data['data']->toArray());die();
        return view('user',$data);
    }
    //个人资料更新
    public function upUser()
    {

        $getData=input('post.');

        $file = request()->file('img_head');//单图--头像
        if ($file){//商品图片是否上传（单图）
            $getData['head']=$this->upload_files($file);
            if (session('appuser.head')){//1.检测原有图片文件
                $this->del_file(session('appuser.head'));//2.清除原有图片文件
            }
        }
        $getData['id']=session('appuser.id');
        AppUserModel::update($getData);
        $updata=['name'=>$getData['nickname']];
        MenuModel::where('user_id',$getData['id'])->update($updata);
        $this->upSess();
        $this->redirect('index');
    }

    //密码修改页面
    public function pass()
    {
        return view('pass');
    }
    //密码修改
    public function setPass(){
        $getData=input('post.');
        //检查旧密码

        if (encryptPass($getData['pass'])!=session('appuser.password')){setDatabaseDef(false,'旧密码不正确');}
       $setAppUser= AppUserModel::update(['password'=>encryptPass($getData['newpass'])],['id'=>session('appuser.id')]);
        $this->upSess();

        return setDatabase($setAppUser,'改密');

    }
    //充值页面
    public function recharge(){
         $list =EquipmentModel::where('equ_user',session('appuser.id'))->select();
         $this->assign('list',$list);
        return view('recharge');
    }
    //微信,支付宝支付页面
    public function recharge11(){
        $list =EquipmentModel::where('equ_user',session('appuser.id'))->select();
        $this->assign('list',$list);
        return view('recharge11');
    }
    //充值
    public function rechargeData()
    {
        $getData=input('post.');
        //判断点卡号和密码
        $where=['name'=>$getData['card_name'],
            'pass'=>$getData['card_pass']];
        $card=CardModel::where($where)->find();//查找点卡
        if (!$card){return retJson('',201,'请检查点卡账号密码');}
        if ($card->display){return retJson('',201,'点卡已使用');}
        $device=EquipmentModel::find($getData['equ_id']);//查找设备
        if (!$device){return retJson('',201,'设备号不存在');}
        $data= $this->recha_time_type($card->type);
        $expire=$device->expire_time;

        if ($expire>time()){//设备还没到期
            $expire+=$data['0'];
        }else{//设备已到期
            $expire=time()+$data['0'];
        }
        //更改点卡状态
        $updata=['id'=>$card->id,'display'=>1];
        $setCard=CardModel::update($updata);
        setDatabaseDef($setCard,'点卡状态更改失败');
        $updata2=['expire_time'=>$expire];
        $setDevice=EquipmentModel::where('id',$device->id)->update($updata2);//该设备增加到期时间
        $inser_En=['num'=>$data['2'],'imei'=>$device['name'],'card_name'=>$getData['card_name'],'addtime'=>time()];
        EnterLogModel::insert($inser_En);
        return setDatabase($setDevice,'充值');
    }
    public function rechargeData1(){
        $data = input('post.');
        //生成订单号
        $ras= rand('100000','999999');//随机生成六位数
        $data['order_sn']=time().$ras;
        switch ($data['leixin']){
            case 'weixin':
                $data['pay_name']='微信支付';
                $this->diangdan($data); //添加订单记录
//                dump($data);die();
                $parameters= $this->wxpay($data['order_sn'],$data['money']);
                return retJson($parameters,200,'充值成功');
            case 'alipayMobile':
                $data['pay_name']='支付宝支付';
                break;
            case 'unionpay':
                $data['pay_name']='银联支付';
                break;
        }
    }
    /**
     * 调用微信H5支付
     * @param $order_sn  订单号
     * @param $money     金额
     * @return $url      支付跳转地址
     */
    public function wxpay($order_sn,$money)
    {
        import('WxPayHelper', EXTEND_PATH . 'Wxpaym');
        $jsApi = new Common_util_pub();
        $parameters = $jsApi->getUrl2($order_sn,$money);
        return $parameters;
    }
    //添加充值记录
    public function diangdan($data){
        $imei=EquipmentModel::where('id',$data['equ_id'])->value('name');//查找设备
        $inser_En=['num'=>'月卡','imei'=>$imei,'card_name'=>$data['order_sn'],'money'=>$data['money'],'data'=>$data['pay_name'],'addtime'=>time()];
        EnterLogModel::insert($inser_En);
    }
}
